package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NCement1DataBo;
import com.ruoyi.zhgd.domain.vo.NCement1DataVo;
import com.ruoyi.zhgd.domain.NCement1Data;
import com.ruoyi.zhgd.mapper.NCement1DataMapper;
import com.ruoyi.zhgd.service.INCement1DataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 水稳拌合站数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NCement1DataServiceImpl implements INCement1DataService {

    private final NCement1DataMapper baseMapper;

    /**
     * 查询水稳拌合站数据
     */
    @Override
    public NCement1DataVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询水稳拌合站数据列表
     */
    @Override
    public TableDataInfo<NCement1DataVo> queryPageList(NCement1DataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NCement1Data> lqw = buildQueryWrapper(bo);
        Page<NCement1DataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水稳拌合站数据列表
     */
    @Override
    public List<NCement1DataVo> queryList(NCement1DataBo bo) {
        LambdaQueryWrapper<NCement1Data> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NCement1Data> buildQueryWrapper(NCement1DataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NCement1Data> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NCement1Data::getBaseCode, bo.getBaseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), NCement1Data::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), NCement1Data::getStationCode, bo.getStationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getMatchCode()), NCement1Data::getMatchCode, bo.getMatchCode());
        lqw.eq(bo.getDischargeTime() != null, NCement1Data::getDischargeTime, bo.getDischargeTime());
        lqw.eq(bo.getBatchWeight() != null, NCement1Data::getBatchWeight, bo.getBatchWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginalId()), NCement1Data::getOriginalId, bo.getOriginalId());
        lqw.eq(bo.getInsertTime() != null, NCement1Data::getInsertTime, bo.getInsertTime());
        return lqw;
    }

    /**
     * 新增水稳拌合站数据
     */
    @Override
    public Boolean insertByBo(NCement1DataBo bo) {
        NCement1Data add = BeanUtil.toBean(bo, NCement1Data.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改水稳拌合站数据
     */
    @Override
    public Boolean updateByBo(NCement1DataBo bo) {
        NCement1Data update = BeanUtil.toBean(bo, NCement1Data.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NCement1Data entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水稳拌合站数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
